<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Dashboard_Creator{
	
	private $_CI;

	
	function __construct(){
		$this->_CI =  &get_instance();
		$this->_CI->load->library("php-ofc-library/open_flash_chart");
		$this->_CI->load->config('xibo', TRUE);
		$this->xiboServerKey=$this->_CI->config->item('serverkey', 'xibo');
		$this->xiboWsdlVersion=$this->_CI->config->item('wsdlversion', 'xibo');
		$this->pieColours = array("#FF0000","#357EC7","#8D38C9","#4CC417","#F87431","#C25A7C","#B4BA22","#8DCDC1","#747E80","#CDCDCD");
	}
	
	function get_active_statistics($data){
	
	}
	
	function getDailyView($id=0){
		$hNum = 0;	
		$chart = new open_flash_chart();
		$chart->set_title(new title('Daily Views'));
		
		if((int)$id==0){
			//dummy data
			$stats=array();
			$stats[0]["title"]="Men Viewers";
			$stats[1]["title"]="Women Viewers";
			$highest=0;
			
			for($param=0;$param<14;$param++){
				$x_data[$param]=date('Y-m-d',mktime(0, 0, 0, date("m")  , date("d")-($param), date("Y")));
				$y_data[$param]=rand()*100;
				if($highest<$y_data[$param]){
					$highest=$y_data[$param];
				}
			}
			$stats[0]['x_data']=$x_data;
			$stats[0]['y_data']=$y_data;
			$stats[0]['hNum']=$highest;
			$stats[0]['colour']="#0077cc";
			
			$highest=0;
			for($param=0;$param<14;$param++){
				$x_data[$param]=date('Y-m-d',mktime(0, 0, 0, date("m")  , date("d")-($param), date("Y")));
				$y_data[$param]=rand()*100;
				if($highest<$y_data[$param]){
					$highest=$y_data[$param];
				}
			}
			$stats[1]['x_data']=$x_data;
			$stats[1]['y_data']=$y_data;
			$stats[1]['hNum']=$highest;
			$stats[1]['colour']="#DB1750";
			//end dummy data
			for($i=0;$i<sizeof($stats);$i++){
				
				$d = new solid_dot();
				$d->size(5)->halo_size(1)->colour($stats[$i]["colour"]);
				$d->tooltip($stats[$i]["title"]."<br>#date:d M Y#"."<br>#val#");
				
				$x_data = $stats[$i]["x_data"];
				$y_data = $stats[$i]["y_data"];
				
				$scatter_values = array();
				//read through data
				for($x=0;$x<sizeof($x_data);$x++){
					$scatter_values[] = new scatter_value(strtotime($x_data[$x]),$y_data[$x]);
				}
				
				$line = new scatter_line($stats[$i]["colour"],"2");
				$line->set_values($scatter_values);
				$line->set_default_dot_style($d);
				
				$chart->add_element($line);
				
				if($stats[$i]["hNum"]>$hNum)
					$hNum = $stats[$i]["hNum"];
			}
		}else{
			$this->_CI->load->model('building_model');
			$this->_CI->building_model->setBuilding($id);
			$building=$this->_CI->building_model->buildingObj;
			$xiboWSDL = new SoapClient('http://'.$building->ip.'/xibo/lib/service/service.wsdl');
			$data = $xiboWSDL->getStatisticsDaily($this->xiboServerKey, '14');
			
			$chart = new open_flash_chart();
			$title="Daily Views";
			$chart->set_title(new title($title));				
			$d = new solid_dot();
			$d->size(5)->halo_size(1)->colour($data["colour"]);
			$d->tooltip($data["title"]."<br>#date:d M Y#"."<br>#val#");
				
			$x_data = $data["x_data"];
			$y_data = $data["y_data"];				
			$scatter_values = array();
				//read through data
			for($x=0;$x<sizeof($x_data);$x++){
				$scatter_values[] = new scatter_value(strtotime($x_data[$x]),$y_data[$x]);
			}	
			$line = new scatter_line($data["colour"],"2");
			$line->set_values($scatter_values);
			$line->set_default_dot_style($d);			
			$chart->add_element($line);
			$hNum = $data["hNum"];		
	    }
		//set tooltips for x-axis 
		$t = new tooltip('');
		$t->set_shadow( false );
		$t->set_stroke(3);
		$t->set_colour( "#A2ACBA" );
		$t->set_background_colour( "#ffffff" );
		$t->set_title_style( "{font-size: 14px;font-weight: bold; color: #555555;}" );
		$t->set_body_style( "{font-size: 10px; color: #555555;}" );

		
		
		//prepare x-axis label
		$x_labels = new x_axis_labels();
		$x_labels->text('#date:d/m#');
		$x_labels->visible_steps(1);
		$x_labels->set_steps(86400);
		
		//prepare x-axis line
		$dates = $data["x_data"];
		$x = new x_axis();
		$x->set_range(strtotime($dates[0]),strtotime($dates[sizeof($dates)-1]));
		$x->set_steps(86400);
		$x->set_labels($x_labels);
		$x->set_grid_colour("#e5e5e5");
		$x->colour("#A2ACBA");
		$x->tick_height(0);
		$x->stroke(3);

		//calculate range data for y axis
		$digit_length = strlen(strval($hNum)) - 1;
		$decimaler = pow(10,$digit_length);
		if($hNum < (4*$decimaler)) $decimaler /= 10;
		
		$ranges = round($hNum/(4*$decimaler)) * $decimaler;
		$y = new y_axis();
		$y->set_range( 0,round($hNum),$ranges);
		$y->set_grid_colour("#e5e5e5");
		$y->set_colour("#A2ACBA");
		$y->set_stroke(3);
		$y->set_tick_length(0);
		
		$chart->set_y_axis( $y );
		$chart->set_x_axis( $x );
		$chart->set_bg_colour('#e6f2fa');
		//$chart->set_tooltip($t);
		return $chart->toPrettyString();		
	}
	function getHourlyView($id){

		

			$this->_CI->load->model('building_model');
			$this->_CI->building_model->setBuilding($id);
			$building=$this->_CI->building_model->buildingObj;
			$xiboWSDL = new SoapClient('http://'.$building->ip.'/xibo/lib/service/service.wsdl');
			$data = $xiboWSDL->getStatisticsHourly($this->xiboServerKey);
	
	$hNum = $data['hNum'];
	if($hNum==0)$hNum=1;
	$data_1 = $data['y_data'];

	$title = new title( "Hourly View" );

	$line_1 = new line();
	$line_1->set_values( $data_1,$data['x_data'] );
	$line_1->set_width( 2 );

	//calculate range data for y axis
	$digit_length = strlen(strval($hNum)) - 1;
	$decimaler = pow(10,$digit_length);
	if($hNum < (4*$decimaler)) $decimaler /= 10;		
	$ranges = round($hNum/(4*$decimaler)) * $decimaler;
	$y = new y_axis();
	$y->set_range( 0,round($hNum),$ranges);
	$x_axis = new x_axis();
	$x_axis->labels = $data['x_data'];
	

	$x_labels = new x_axis_labels();
	$x_labels->text('#val#');
	$x_labels->labels = $data['x_data'];
	// Add the X Axis Labels to the X Axis
	$x_axis->set_labels( $x_labels );

	$chart = new open_flash_chart();
	$chart->set_title( $title );
	$chart->add_element( $line_1 );
	$chart->set_y_axis( $y );
	$chart->set_x_axis( $x_axis );
	return $chart->toPrettyString();
			/*
			$chart = new open_flash_chart();
			$title="Hourly Views";
			$chart->set_title(new title($title));				
			$d = new solid_dot();
			$d->size(5)->halo_size(1)->colour($data["colour"]);
			$d->tooltip($data["title"]."<br>#val#");
				
			$x_data = $data["x_data"];
			$y_data = $data["y_data"];				
			$scatter_values = array();
				//read through data
			for($x=0;$x<sizeof($x_data);$x++){
				$scatter_values[] = new scatter_value($x,$y_data[$x]);
			}	
			$line = new scatter_line($data["colour"],"2");
			$line->set_values($scatter_values);
			$line->set_default_dot_style($d);			
			$chart->add_element($line);
			$hNum = $data["hNum"];		
	    
		//set tooltips for x-axis 
		$t = new tooltip('');
		$t->set_shadow( false );
		$t->set_stroke(3);
		$t->set_colour( "#A2ACBA" );
		$t->set_background_colour( "#ffffff" );
		$t->set_title_style( "{font-size: 14px;font-weight: bold; color: #555555;}" );
		$t->set_body_style( "{font-size: 10px; color: #555555;}" );

		
		
		//prepare x-axis label
		$x_labels = new x_axis_labels();
		$x_labels->text('');
		$x_labels->visible_steps(1);
		$x_labels->set_steps(86400);
		
		//prepare x-axis line
		$dates = $data["x_data"];
		$x = new x_axis();
		$x->set_range(0,6);
		$x->set_steps(86400);
		$x->set_labels($x_labels);
		$x->set_grid_colour("#e5e5e5");
		$x->colour("#A2ACBA");
		$x->tick_height(0);
		$x->stroke(3);

		//calculate range data for y axis
		$digit_length = strlen(strval($hNum)) - 1;
		$decimaler = pow(10,$digit_length);
		if($hNum < (4*$decimaler)) $decimaler /= 10;
		
		$ranges = round($hNum/(4*$decimaler)) * $decimaler;
		$y = new y_axis();
		$y->set_range( 0,round($hNum),$ranges);
		$y->set_grid_colour("#e5e5e5");
		$y->set_colour("#A2ACBA");
		$y->set_stroke(3);
		$y->set_tick_length(0);
		
		$chart->set_y_axis( $y );
		$chart->set_x_axis( $x );
		$chart->set_bg_colour('#e6f2fa');
		//$chart->set_tooltip($t);
		
		return $chart->toPrettyString();		
		*/
	}	
	function getTopLocations(){
		//$this->_CI->load->model("dashboard_model");
		
		//if(!($data = $this->_CI->dashboard_model->get_top_used_browser())) return false;
		
		$title = new title("Top Locations");
		$chart = new open_flash_chart();
		$chart->set_title($title);
		$chart->set_bg_colour('#e6f2fa');
		$data=array();
		$data[0]['location']="FX Lifestyle";
		$data[0]['total']=rand()*100;
		$data[1]['location']="Grand Indonesia";
		$data[1]['total']=rand()*100;
		$data[2]['location']="Mall Of Indonesia";
		$data[2]['total']=rand()*100;
		$data[3]['location']="Setiabudi One";
		$data[3]['total']=rand()*100;
		$data[4]['location']="Senayan City";
		$data[4]['total']=rand()*100;
		$i=0;
		$pieval = array();
		foreach($data as $val){
			$pieval[$i]=new pie_value(intval($val['total']),$val['location']);
			$pieval[$i]->set_colour($this->pieColours[$i]);
			$i++;
			if($i==6) break;
		}
		
		$pie = new pie();
		$pie->set_values($pieval);
		$pie->set_tooltip( '#label# <br> <b>#percent#</b>');
		
		
		$chart->add_element( $pie );
		
		return $chart->toPrettyString();
		
	}
	function getGender($id=0){
		//$this->_CI->load->model("dashboard_model");
		
		//if(!($data = $this->_CI->dashboard_model->get_top_used_browser())) return false;
		
		$title = new title("Gender Percentage");
		$chart = new open_flash_chart();
		$chart->set_title($title);
		$chart->set_bg_colour('#e6f2fa');
		$data=array();
		if($id==0){
			$data[0]['location']="Male";
			$data[0]['total']=rand()*100;
			$data[1]['location']="Female";
			$data[1]['total']=rand()*100;
			$data[2]['location']="Unisex";
			$data[2]['total']=rand()*100;
		}else{
			$this->_CI->load->model('building_model');
			$this->_CI->building_model->setBuilding($id);
			$building=$this->_CI->building_model->buildingObj;
			$xiboWSDL = new SoapClient('http://'.$building->ip.'/xibo/lib/service/service.wsdl');
			$dataXibo = $xiboWSDL->getGenderStatistic($this->xiboServerKey);
			$data[0]['location']="Male";
			$data[0]['total']=$dataXibo["male"];
			$data[1]['location']="Female";
			$data[1]['total']=$dataXibo["female"];
			if($dataXibo["uni"] != 0){
				$data[2]['location']="Unisex";
				$data[2]['total']=$dataXibo["uni"];
			}
		}
		$i=0;
		$pieval = array();
		foreach($data as $val){
			$pieval[$i]=new pie_value(intval($val['total']),$val['location']);
			$pieval[$i]->set_colour($this->pieColours[$i]);
			$i++;
		}
		
		$pie = new pie();
		$pie->set_values($pieval);
		$pie->set_tooltip( '#label# <br> <b>#percent#</b>');
		
		
		$chart->add_element( $pie );
		
		return $chart->toPrettyString();
		
	}
	function getPeakHours(){
		//$this->_CI->load->model("dashboard_model");
		
		//if(!($data = $this->_CI->dashboard_model->get_top_used_browser())) return false;
		
		$title = new title("Peak Hours");
		$chart = new open_flash_chart();
		$chart->set_title($title);
		$chart->set_bg_colour('#e6f2fa');
		$data=array();
		$data[0]['location']="10-12";
		$data[0]['total']=rand()*100;
		$data[1]['location']="12-14";
		$data[1]['total']=rand()*100;
		$data[2]['location']="14-16";
		$data[2]['total']=rand()*100;
		$data[3]['location']="16-18";
		$data[3]['total']=rand()*100;
		$data[4]['location']="18-20";
		$data[4]['total']=rand()*100;
		$i=0;
		$pieval = array();
		foreach($data as $val){
			$pieval[$i]=new pie_value(intval($val['total']),$val['location']);
			$pieval[$i]->set_colour($this->pieColours[$i]);
			$i++;
			if($i==6) break;
		}
		
		$pie = new pie();
		$pie->set_values($pieval);
		$pie->set_tooltip( '#label# <br> <b>#percent#</b>');
		
		
		$chart->add_element( $pie );
		
		return $chart->toPrettyString();
		
	}
	function getDays(){
		//$this->_CI->load->model("dashboard_model");
		
		//if(!($data = $this->_CI->dashboard_model->get_top_used_browser())) return false;
		
		$title = new title("Peak Days");
		$chart = new open_flash_chart();
		$chart->set_title($title);
		$chart->set_bg_colour('#e6f2fa');
		$data=array();
		$data[0]['location']="Monday";
		$data[0]['total']=rand()*100;
		$data[1]['location']="Tuesday";
		$data[1]['total']=rand()*100;
		$data[2]['location']="Wednesday";
		$data[2]['total']=rand()*100;
		$data[3]['location']="Thursday";
		$data[3]['total']=rand()*100;
		$data[4]['location']="Friday";
		$data[4]['total']=rand()*100;
		$data[4]['location']="Saturday";
		$data[4]['total']=rand()*100;
		$data[4]['location']="Sunday";
		$data[4]['total']=rand()*100;
		$i=0;
		$pieval = array();
		foreach($data as $val){
			$pieval[$i]=new pie_value(intval($val['total']),$val['location']);
			$pieval[$i]->set_colour($this->pieColours[$i]);
			$i++;
			
		}
		
		$pie = new pie();
		$pie->set_values($pieval);
		$pie->set_tooltip( '#label# <br> <b>#percent#</b>');
		
		
		$chart->add_element( $pie );
		
		return $chart->toPrettyString();
		
	}
}